package com.gzist.security4.controller;

import com.gzist.security4.domain.Role;
import com.gzist.security4.domain.User;
import com.gzist.security4.domain.dto.LoginParams;
import com.gzist.security4.service.UserService;
import com.gzist.security4.utils.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private UserService userService;

    /*
     * @description:
     *  登录接口
     **/
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@ApiParam("登录参数") @RequestBody LoginParams loginParams) {
        String token = userService.login(loginParams);
        log.info("登录操作==》{}",token);
        return Result.success(token);
    }

    /*
     * @description:
     *  获取用户列表
     **/
    @ApiOperation("获取用户列表")
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/list")
    public Result list() {
        List<User> list = userService.list();
        log.info("操作==》{}",list);
        return Result.success(list);
    }

    /*
     * @description:
     * 获取单个用户信息
     **/
    @ApiOperation("获取单个用户信息")
    @PreAuthorize("hasAuthority('user:selectOne')")
    @GetMapping("/selectOne")
    public Result selectOne(@ApiParam("用户id") @RequestParam Long id) {
        User user = userService.getById(id);
        log.info("操作==》{}",user);
        return Result.success(user);
    }

    /*
     * @description:
     * 根据用户id获取其下所拥有的所有角色
     **/
    @ApiOperation("根据用户id获取所有角色")
    @PreAuthorize("hasAuthority('user:getAllRoles')")
    @GetMapping("/getAllRoles")
    public Result getAllRoles(@ApiParam("用户id") @RequestParam Long id) {
        List<Role> roles = userService.getAllRoles(id);
        log.info("操作==》{}",roles);
        return Result.success(roles);
    }
}
